// Created by Roman Novitsky on 03.06.2009


#region Imported Namespaces


using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

using WtfManager.Controls;


#endregion


namespace WtfManager.UIManager
{
  public static class FrameworkElementExtensions
  {
    #region Methods


    public static List<object> GetDataItems(this FrameworkElement frameworkElement)
    {
      var items = new List<object>();

      if (frameworkElement is ListBox)
      {
        var listView = (ListBox)frameworkElement;

        IList selectedItems = listView.SelectedItems;

        foreach (object item in selectedItems)
        {
          items.Add(item);
        }
      }
      else if (frameworkElement is TreeView)
      {
        var treeView = (TreeView)frameworkElement;

        items.Add(treeView.SelectedItem);
      }
      else if (frameworkElement is Selector)
      {
        var selector = (Selector)frameworkElement;

        if (selector.SelectedItem != null)
        {
          items.Add(selector.SelectedItem);
        }
      }
      else if (frameworkElement is ContentControl)
      {
        var contentControl = (ContentControl)frameworkElement;
        items.Add(contentControl.Content);
      }
      else
      {
        items.Add(frameworkElement.DataContext);
      }

      return items;
    }


    public static object GetContent(this FrameworkElement frameworkElement)
    {
      if (frameworkElement is WtfDocumentContent)
      {
        var documentContent = (WtfDocumentContent)frameworkElement;
        return documentContent.Data;
      }

      if (frameworkElement is ContentControl)
      {
        var contentControl = ((ContentControl)frameworkElement);
        return contentControl.Content;
      }

      return null;
    }


    #endregion
  }
}